//
//  PywCommonPywLYSDKApi.m
//  PywCommonSDKDemo
//
//  Created by yingxin ye on 2017/8/14.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonPywLYSDKApi.h"
#import <UIKit/UIKit.h>
#import "RuntimeInvoker.h"
#import "PywCommonSDKParameter.h"
#import "PywCommonMutableDictionary.h"
#import "PywCommonPlatformInfo.h"
#import "PywCommonPywLYDataPackHelper.h"
#import "PywSdk.h"

@implementation PywCommonPywLYSDKApi


+ (void)initialize
{
    if (self == [PywCommonPywLYSDKApi class])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(inited:)
                                                     name:@"k_pyw_cp_init_notification"    // 初始化完成
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(logined:)
                                                     name:@"k_pyw_cp_login_notification"   // 登录完成
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(logouted:)
                                                     name:@"k_pyw_cp_logout_notification"  // 注销完成
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(payed:)
                                                     name:@"k_pyw_cp_pay_notification"     // 固定支付完成
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(payed:)
                                                     name:@"k_pyw_cp_anypay_notification"  // 任意支付完成
                                                   object:nil];
    }
}

/// 初始化完成
+ (void)inited:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    int result = [[dict objectForKey:@"result"] intValue];
    NSString *info = [dict objectForKey:@"info"];
    
    // 判断初始化成功与否
    if (result == 0) {
        COMLog(@"%@！",info);
        NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];
        [mDict setObject:[NSNumber numberWithInt:CommonSDK_InitState_Success] forKey:@"result"];
        [mDict setObject:@"初始化成功！" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_InitNotification
                                                            object:nil
                                                          userInfo:mDict];
    } else {
        COMLog(@"%@！",info);
        NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];
        [mDict setObject:[NSNumber numberWithInt:CommonSDK_InitState_Faliure] forKey:@"result"];
        [mDict setObject:@"初始化失败！" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_InitNotification
                                                            object:nil
                                                          userInfo:mDict];
    }
}

/**
 * 登录完成
 */
+ (void)logined:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    int result = [[dict objectForKey:@"result"] intValue];
    NSString *info = [dict objectForKey:@"info"];
    
    NSString *user_uid = [self getUid];
    NSString *user_token = [self getToken];
    NSString *account = [dict objectForKey:@"account"];
    
    if (result == 0) {
        COMLog(@"%@！ user_token = %@  user_uid = %@",info, user_token, user_uid);
        //将账号缓存起来
        [PywCommonPlatformInfo sharedCommonPlatformInfo].common_account = account;
        PywCommonMutableDictionary *mDict = [PywCommonMutableDictionary dictionary];
        [mDict setObject:[NSNumber numberWithInt:CommonSDK_LoginState_Success] forKey:@"result"];
        [mDict setObject:@"登录成功！" forKey:@"info"];
        [mDict setObject:user_uid forKey:@"userid"];
        [mDict setObject:user_token forKey:@"token"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_LoginNotification
                                                            object:nil
                                                          userInfo:mDict];
        
    }else {
        COMLog(@"%@！",info);
        // 发送回调
        NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];
        [mDict setObject:[NSNumber numberWithInt:CommonSDK_LoginState_Faliure] forKey:@"result"];
        [mDict setObject:@"登录失败！" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_LoginNotification
                                                            object:nil
                                                          userInfo:mDict];
    }
}


/**
 *  注销完成
 */
+ (void)logouted:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    int result = [[dict objectForKey:@"result"] intValue];
    NSString *info = [dict objectForKey:@"info"];
    
    if (result == 0) {
        COMLog(@"%@！",info);
        // 发送回调
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:[NSString stringWithFormat:@"%d",CommonSDK_LogoutState_Finish] forKey:@"result"];
        [dict setValue:@"注销成功" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_LogoutNotification
                                                            object:nil
                                                          userInfo:dict];
    }
}

// 支付完成
+ (void)payed:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    int result = [[dict objectForKey:@"result"] intValue];
    NSString *info = [dict objectForKey:@"info"];
    
    if (result == 0) {
        COMLog(@"%@！",info);
        // 发送回调
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:[NSString stringWithFormat:@"%d",CommonSDK_PayState_Finish] forKey:@"result"];
        [dict setValue:@"支付完成！" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_PayNotification
                                                            object:nil
                                                          userInfo:dict];
    }
}


/**
 初始化SDK
 */
+ (void)channelApi_initWithParam:(NSDictionary*)p
{
    NSString *sdk_api_url = [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_channel_apiurl;
    NSString *sdk_pay_url = [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_pay_apiurl;
    NSString *gamekey = [PywCommonPlatformInfo sharedCommonPlatformInfo].gamekey;
    NSString *th_id = [PywCommonPlatformInfo sharedCommonPlatformInfo].channel_id;
    NSString *sdk_type = [PywCommonPlatformInfo sharedCommonPlatformInfo].sdk_type;
    PywInitConfigure *config = [[PywInitConfigure alloc] init];
    // 游戏的Key
    config.gamekey = gamekey;
    config.sdk_api_url = sdk_api_url;
    config.sdk_pay_url = sdk_pay_url;
    config.th_ch_id = th_id;
    if(sdk_type && sdk_type.length>0){
        config.pyw_sdk_type = [sdk_type intValue];
    }
    // 主登录页面，点击登录窗以外空白处，是否隐藏主登录窗【默认为：YES】
    config.canHideLogin = NO;
    [[PywSdk manger] initialize:config isSplash:NO];
}

/**
 登录SDK
 */
+ (void)channelApi_ShowLogin
{
    [[PywSdk manger]login];
}

/**
 设置悬浮穿的显示与隐藏
 
 @param status 当status = YES时，显示悬浮窗；当status = NO时，不显示悬浮窗；
 */
+ (void)channelApi_ShowOrHideFloatView:(BOOL)status
{
    if (status == YES)
    {
        [[PywSdk manger] showFloat];
    }
    else
    {
        [[PywSdk manger] hideFloat];
    }
}

/**
 注销SDK
 */
+ (void)channelApi_ShowLogout
{
    [[PywSdk manger]logout];
}

/**
 *  显示用户中心
 */
+ (void)channelApi_ShowUserCenter
{
    [[PywSdk manger] showCenter];
}

/**
 *
 *  @param payInfo 购买产品信息
 */
+ (void)channelApi_StartPay:(NSDictionary *)payInfo
{
    // 当商品信息为空时，跳出
    if (payInfo.count == 0) return;
    
    // 根据是否传入有“商品ID”和支付类型来判断，使用的是定额支付，还是任意支付
    NSString *cpprodid = @"";
    if ([[payInfo allKeys] containsObject:@"cpprodid"]) {
        cpprodid = [payInfo objectForKey:@"cpprodid"];
    }
    NSString *charge_type = @"";
    if ([[payInfo allKeys] containsObject:@"charge_type"]) {
        charge_type = [payInfo objectForKey:@"charge_type"];
    }
    
    // 判断支付方式为 定额支付 | 任意支付
    if (cpprodid.length > 0 && [charge_type isEqualToString:@"1"]) { // 固定支付
        [[PywSdk manger] pay:payInfo];
    }else{ // 任意支付
        [[PywSdk manger] anyPay:payInfo];
    }
}

/**
 切换应用运行模式
 
 @param isForeground 当isForeground = YES, 应用前台运行；当isForeground = NO, 应用后台运行或退出程序；
 @param application 当前应用
 */
+ (void)channelApi_IsWillEnterForeground:(BOOL)isForeground OtherWiseEnterBackgroundOrExitApplication:(UIApplication *)application
{
    if(isForeground == YES)
    {
        [[PywSdk manger] didWillEnterForeground];
    }
    else
    {
        [[PywSdk manger] didEnterBackgroundOrExitApplication];
    }
}


/**
 必调：当app返回时传的参数 保存cookie到沙盒
 */
+(void)channelApi_OpenUrl:(NSURL*)url
{
    
}


/**
 获取SDK版本信息
 */
+ (NSString *)channelApi_GetPlatformData
{
    return [[PywSdk manger] getSdkVersion];
}

/**
 提交玩家数据
 
 @param playerInfo 玩家数据
 */
+ (void)channelApi_SetPlayerInfo:(NSDictionary *)playerInfo
{
    [[PywSdk manger] sdk_SetRoleInfo:playerInfo];
}

/**
 获取token
 */
+(NSString*)getToken
{
   return [PywSdk manger].token;
}

/**
 获取uid
 */
+(NSString*)getUid
{
    return [PywSdk manger].uid;
}

@end
